package com.system.healthproject.model.admin.controller;

import com.system.healthproject.bean.PagesBean;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.exception.TokenException;
import com.system.healthproject.model.admin.paramsbean.LoginParams;
import com.system.healthproject.model.admin.service.AdminUserService;
import com.system.healthproject.utils.JwtUtil;
import com.system.healthproject.utils.RedisUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Api(value = "后端用户管理",tags = "后端-用户管理API")
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    RedisUtil redisUtil;


    @ApiOperation(value = "后端用户登录接口(后台已调)",notes = "验证后端用户的用户名和密码",httpMethod = "post")
    @PostMapping(value = "/login",produces = "application/json;charset=utf-8")
    public ApiResult<HashMap<String, Object>> login(
            @Validated
            @RequestBody LoginParams loginParams){
        HashMap<String, Object> login = adminUserService.login(loginParams);
        if(login!=null){
            return ApiResult.successWithData("成功",login);
        }else {
            return ApiResult.failure("登录失败,请重新登录");
        }
    }

    @ApiOperation(value = "后端获取管理员列表",notes = "后端获取管理员列表",httpMethod = "post")
    @PostMapping(value = "/adminList",produces = "application/json;charset=utf-8")
    public PagesBean<Map<String, Object>> adminList(
            @NotNull @RequestParam(value = "page",required = false) Integer page,
            @NotNull @RequestParam(value = "size",required = false) Integer size){

        PagesBean<Map<String, Object>> mapPagesBean = adminUserService.adminList(page, size);
        return mapPagesBean;
    }


    @ApiOperation(value = "运营人员添加运营人员(后台已调)",response = ApiResult.class)
    @PostMapping(value = "/addAdminUser",produces = "application/json;charset=utf-8")
    public ApiResult<String> addAdminUser(
            @Validated
            @RequestBody
            @ApiParam(name = "user",value = "后端用户名和密码",required = true)
                    LoginParams loginParams){
        Boolean aBoolean = adminUserService.addAdminUser(loginParams);
        return ApiResult.withBoolean(aBoolean,"添加管理员成功","添加管理员失败");
    }

    @ApiOperation(value = "后端验证token是否有效",response = ApiResult.class)
    @PostMapping(value = "/verificationToken",produces = "application/json;charset=utf-8")
    public ApiResult verificationToken(@NotBlank @RequestParam(required = false,value = "token") String token) throws TokenException {
        Map<String, Object> map = JwtUtil.validateToken(token);
        if(map==null){
            throw new TokenException("token错误,请重新登录");
        }
        boolean b = redisUtil.hasKey(token);
        System.out.println(b);
        Map<String, Object> userInfo = (Map<String, Object>) redisUtil.get(token);
        long expire = redisUtil.getExpire(token);
        System.out.println(expire);
        if(userInfo==null||userInfo.size()==0){
            throw new TokenException("token失效,请重新登录");
        }
        return ApiResult.success("验证成功");
    }


    /**
     * 管理员账号的停用/启用
     * @param
     * @return
     */
    @RequestMapping(value = "/stopAdminStartUp" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "管理员账户停用/启用(接口已调用)",notes = "管理员账号医生停用/启用",httpMethod = "post",response = ApiResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginId",value = "管理员ID",paramType = "query",dataType = "int",required = true) ,
            @ApiImplicitParam(name = "state",value = "状态",paramType = "query",dataType = "int",required = true) ,
    })
    @ResponseBody
    public ApiResult stopAdminStartUp(Integer loginId,Integer state){
        boolean tf = adminUserService.AdminstopAndStartUp(loginId,state);
        ApiResult apiResult=new ApiResult();

        if (tf==true){
            apiResult.setMsg("成功");
            apiResult.setCode(200);
            return apiResult;
        }

        apiResult.setMsg("失败");
        apiResult.setCode(500);
        return apiResult;
    }






}
